package pais.www.stat;

import java.io.PrintWriter;
import java.util.StringTokenizer;

import pasys.error.PaposException;


/**
 * StatCommands ; Process show-me-the-stat commands
 */
public class Commands {
  public static final String SECRET_COMMAND_PREFIX = "show-me-the-stat";
  public static void printStat(PrintWriter toClient, String query) {
    StringTokenizer stok = new StringTokenizer(query, " ");
    stok.nextToken(); // eat show-me-the-stat
    toClient.println("magic command detected : ");

    toClient.println("<pre>");

    if (stok.hasMoreTokens()) {
      String command = stok.nextToken();
      
      try {
        if (command.equals("all-keys")) {
          PrintAllKeys.printKeys(toClient, stok);
        } else if (command.equals("url-stat")) {
          PrintUrlStat.printUrlStat(toClient, stok);
        } else if (command.equals("key-stat")) {
          PrintKeyStat.printKeyStat(toClient, stok);
        } else if (command.equals("url-keys")) {
          PrintUrlKeys.printPageKeywords(toClient, stok);
        } else if (command.equals("url-info")) {
          PrintUrlInfo.printURL(toClient, stok);
        }
      } catch (PaposException e) {
        toClient.println(e.toString());
      }
    } else {
      printStatUsage(toClient);
    }			
    toClient.println("</pre>");
  }

  public static void printStatUsage(PrintWriter toClient) {
    toClient.println("Usage:"+SECRET_COMMAND_PREFIX+" command");
    toClient.println("  < list of commands >");
    toClient.println("     * url-stat ");
    toClient.println("     * key-stat ");
    toClient.println("     * all-keys [skip_count]");
    toClient.println("     * url-keys [url|uid]");
    toClient.println("     * url-info [url|uid]");
  }
}
